package cn.com.woxitech.dev.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.woxitech.dev.dao.gen.WmUserFollowMapper;
import cn.com.woxitech.dev.dao.gen.WmUserMapper;
import cn.com.woxitech.dev.model.WeixinWebAccessToken;
import cn.com.woxitech.dev.model.gen.WmRoomExample;
import cn.com.woxitech.dev.model.gen.WmUser;
import cn.com.woxitech.dev.model.gen.WmUserExample;
import cn.com.woxitech.dev.model.gen.WmUserFollow;
import cn.com.woxitech.dev.model.gen.WmUserFollowExample;

@Service
public class UserService {

	@Autowired
	WmUserMapper userMapper;
	
	@Autowired
	WmUserFollowMapper userFollowMapper;
	
	public WmUser getUserById(int id) {
		return userMapper.selectByPrimaryKey(id);
	}
	public WmUser getUserByUnionId(String unionId) {
		WmUserExample example = new WmUserExample();
		example.createCriteria().andWxUnionidEqualTo(unionId);
		List<WmUser> list = userMapper.selectByExample(example);
		if (list.isEmpty()) {
			
			WmUserExample e2 = new WmUserExample();
			e2.createCriteria().andUuidEqualTo(unionId);
			List<WmUser> l2 = userMapper.selectByExample(e2);
			if(l2.isEmpty()){
				return null;
			}else{
				return l2.get(0);
			}
			
		}else {
			return list.get(0);
		}
	}
	public List<WmUser> getAllUsers() {
		WmUserExample example = new WmUserExample();
		return userMapper.selectByExample(example);
	}
	public void insertUser(WmUser user) {
		userMapper.insertSelective(user);
	}
	public void updateUser(WmUser user) {
		userMapper.updateByPrimaryKeySelective(user);
	}
	public WmUser saveWeixinWebAccessToken(WeixinWebAccessToken access_token) {
		// TODO Auto-generated method stub
		WmUser user = new WmUser();
		WmUserExample e = new WmUserExample();
		
		if(access_token.getUnionid() == null){
			e.createCriteria().andWxOpenidEqualTo(access_token.getOpenid());	
		}else{
			e.createCriteria().andWxUnionidEqualTo(access_token.getUnionid());
		}
		
		
		List<WmUser> temp_list = userMapper.selectByExample(e);
		if(temp_list == null || temp_list.isEmpty()){
			//创建一个WmUser
			user.setWxOpenid(access_token.getOpenid());
			user.setWxUnionid(access_token.getUnionid());
			user.setuAccessToken(access_token.getAccess_token());
			user.setuExpiresIn(access_token.getExpires_in());
			user.setuRefreshToken(access_token.getRefresh_token());
			user.setuScope(access_token.getScope());
			
			user.setCreatetime(new Date(System.currentTimeMillis()));
			user.setLastlogin(new Date(System.currentTimeMillis()));
			user.setUuid(UUID.randomUUID().toString());
			user.setNeedUpdate("3");
			this.insertUser(user);
		}else{
			//更新WmUser
			user = temp_list.get(0);
			user.setWxOpenid(access_token.getOpenid());
			if(access_token.getUnionid() != null){
				user.setWxUnionid(access_token.getUnionid());
			}
			user.setuAccessToken(access_token.getAccess_token());
			user.setuExpiresIn(access_token.getExpires_in());
			user.setuRefreshToken(access_token.getRefresh_token());
			user.setuScope(access_token.getScope());
			
			this.updateUser(user);
		}
		
		return user;
	}
	public void updateUserByUuid(WmUser user,String uuid) {
		WmUserExample example = new WmUserExample();
		example.createCriteria().andUuidEqualTo(uuid);
		userMapper.updateByExampleSelective(user, example);
	}
	public WmUser getUserByUuid(String userUuid) {
		// TODO Auto-generated method stub
		WmUserExample example = new WmUserExample();
		example.createCriteria().andUuidEqualTo(userUuid);
		List<WmUser> list = userMapper.selectByExample(example);
		if (list.isEmpty()) {
			return null;
		}else {
			return list.get(0);
		}
	}
	public void updateUserByOpenid(WmUser user, String openid) {
		// TODO Auto-generated method stub
		WmUserExample example = new WmUserExample();
		example.createCriteria().andWxOpenidEqualTo(openid);
		userMapper.updateByExampleSelective(user, example);
	}
	
	public void updateUserByUnionid(WmUser user, String unionid) {
		// TODO Auto-generated method stub
		WmUserExample example = new WmUserExample();
		example.createCriteria().andWxUnionidEqualTo(unionid);
		userMapper.updateByExampleSelective(user, example);	
	}
	
	public int doesUserFollowUser(String from_useruuid, String to_useruuid){
		WmUserFollowExample e = new WmUserFollowExample();
		e.createCriteria().andFromuseruuidEqualTo(from_useruuid).andTouseruuidEqualTo(to_useruuid);
		int c = userFollowMapper.countByExample(e);
		return (c > 0)?1:0;
	}
	public void addFollowUser(String fromuseruuid, String touseruuid) {
		// TODO Auto-generated method stub
		
		if(doesUserFollowUser(fromuseruuid, touseruuid) == 0){
			WmUserFollow w = new WmUserFollow();
			
			w.setFollowtime(new Date(System.currentTimeMillis()));
			w.setFromuseruuid(fromuseruuid);
			w.setTouseruuid(touseruuid);
			
			userFollowMapper.insertSelective(w);
		}
		
	}
	public void removeFollowUser(String fromuseruuid, String touseruuid) {
		// TODO Auto-generated method stub
		
		if(doesUserFollowUser(fromuseruuid, touseruuid) == 1){
			
			WmUserFollowExample e = new WmUserFollowExample();
			e.createCriteria().andFromuseruuidEqualTo(fromuseruuid).andTouseruuidEqualTo(touseruuid);
			
			userFollowMapper.deleteByExample(e);
		}

	}
	public int getFollowingCount(String useruuid) {
		// TODO Auto-generated method stub
		
		WmUserFollowExample e = new WmUserFollowExample();
		e.createCriteria().andFromuseruuidEqualTo(useruuid);
		return this.userFollowMapper.countByExample(e);
	}
	
	public int getFollowerCount(String useruuid) {
		// TODO Auto-generated method stub

		WmUserFollowExample e = new WmUserFollowExample();
		e.createCriteria().andTouseruuidEqualTo(useruuid);
		return this.userFollowMapper.countByExample(e);
		
	}
	
	
	public List<String> getFollowingUsersStrsByUuid(String useruuid) {
		// TODO Auto-generated method stub
		
		WmUserFollowExample e = new WmUserFollowExample();
		e.createCriteria().andFromuseruuidEqualTo(useruuid);
		List<WmUserFollow> uflist = this.userFollowMapper.selectByExample(e);
		
		if(uflist.isEmpty()){
			return new ArrayList<String>();
		}
		
		List<String> list_str = new ArrayList<String>();
		
		for(WmUserFollow wuf : uflist){
			list_str.add(wuf.getTouseruuid());
		}

		return list_str;
	}
	
	public List<WmUser> getFollowingUsersByUuid(String useruuid) {
		// TODO Auto-generated method stub
		
		WmUserFollowExample e = new WmUserFollowExample();
		e.createCriteria().andFromuseruuidEqualTo(useruuid);
		List<WmUserFollow> uflist = this.userFollowMapper.selectByExample(e);
		
		if(uflist.isEmpty()){
			return new ArrayList<WmUser>();
		}
		
		List<String> list_str = new ArrayList<String>();
		
		for(WmUserFollow wuf : uflist){
			list_str.add(wuf.getTouseruuid());
		}

		WmUserExample e_user = new WmUserExample();
		e_user.createCriteria().andUuidIn(list_str);
		e_user.setOrderByClause("lastlogin desc");
		List<WmUser> list = this.userMapper.selectByExample(e_user);
		return list;
	}
	public List<WmUser> getFollowerUsersByUuid(String useruuid) {
		// TODO Auto-generated method stub
		WmUserFollowExample e = new WmUserFollowExample();
		e.createCriteria().andTouseruuidEqualTo(useruuid);
		List<WmUserFollow> uflist = this.userFollowMapper.selectByExample(e);
		
		if(uflist.isEmpty()){
			return new ArrayList<WmUser>();
		}
		
		List<String> list_str = new ArrayList<String>();
		
		for(WmUserFollow wuf : uflist){
			list_str.add(wuf.getFromuseruuid());
		}
		
		WmUserExample e_user = new WmUserExample();
		e_user.createCriteria().andUuidIn(list_str);
		e_user.setOrderByClause("lastlogin desc");
		List<WmUser> list = this.userMapper.selectByExample(e_user);
		return list;
	}
	public int countAllUsersBySearchKeywords(String w, int _p, int _s) {
		// TODO Auto-generated method stub
		
		WmUserExample e = new WmUserExample();
		e.createCriteria().andNameLike("%"+w+"%");//.andEmailLike("%"+w+"%").andTelLike("%"+w+"%");
		e.setOrderByClause("createtime desc limit "+((_p-1)*_s)+","+_s);
		
		return this.userMapper.countByExample(e);
	}
	public List<WmUser> getAllUsersBySearchKeywords(String w, int _p, int _s) {
		// TODO Auto-generated method stub
		WmUserExample e = new WmUserExample();
		e.createCriteria().andNameLike("%"+w+"%");//.andEmailLike("%"+w+"%").andTelLike("%"+w+"%");
		e.setOrderByClause("createtime desc limit "+((_p-1)*_s)+","+_s);
		
		return this.userMapper.selectByExample(e);
	}
	public WmUser getUserByEmail(String email) {
		// TODO Auto-generated method stub
		WmUserExample e = new WmUserExample();
		e.createCriteria().andEmailEqualTo(email);
		List<WmUser> l = this.userMapper.selectByExample(e);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return l.get(0);
		}
	}
	public WmUser getUserByTel(String tel) {
		// TODO Auto-generated method stub
		WmUserExample e = new WmUserExample();
		e.createCriteria().andTelEqualTo(tel);
		List<WmUser> l = this.userMapper.selectByExample(e);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return l.get(0);
		}
	}
	public List<WmUser> getUsersByUsertype(String usertype, int size, int pos, String refreshtype, String tel) {
		// TODO Auto-generated method stub

		WmUserExample e = new WmUserExample();
		cn.com.woxitech.dev.model.gen.WmUserExample.Criteria criteria = e.createCriteria();
		
		if(usertype == null || usertype.equals("") || usertype.equals("null")){
			usertype = null;
		}

		if(tel == null || tel.equals("") || tel.equals("null")){
			if(usertype == null){
				criteria.andUserTypeIsNull();	
			}else{
				criteria.andUserTypeIsNotNull().andUserTypeEqualTo(usertype);
			}
			
			criteria.andTelIsNotNull().andTelNotEqualTo("");
			
		}else{
			criteria.andTelEqualTo(tel);
						
		}
		
		e.setOrderByClause("lastlogin" + " desc limit " + pos + "," + size);
		
		return userMapper.selectByExample(e);
		
	}
	public void updateUserHard(WmUser user) {
		// TODO Auto-generated method stub
		this.userMapper.updateByPrimaryKey(user);
	}
}
